package com.me.control;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.me.constant.Constant;
import com.me.enums.ScreenEnum;
import com.me.helper.PreferencesData;
import com.me.memorytest.MainMemoryTest;
import com.me.ui.HeaderGame;

public class CreateWindown {
	float w;
	float h;
	static Group group;
	ScrollPane scroll;
	Label lb_about;
	
	public Image createBackground(){
		TextureRegionDrawable bg = new TextureRegionDrawable(MainMemoryTest.getAtlas().findRegion("datagame/bg_popup"));
		Image img = new Image(bg);
		return img;
	}
	public final Image img_backgroundHide = createBackground();
	
	public static Group createWindown_showResult(){
		group = new Group();
		
		Image img_bg = new Image(Constant.atlas.findRegion("datagame/bg_result"));
		img_bg.setWidth(600);
		img_bg.setX((Constant.STAGE_WIDTH - img_bg.getWidth())/2);
		img_bg.setY((Constant.STAGE_HEIGHT - img_bg.getHeight())/2);
		
		Label lb_result = new Label("Level clear!", Constant.skin, "label45_bold");
		lb_result.setX((Constant.STAGE_WIDTH - lb_result.getWidth())/2);
		lb_result.setY(535);
		
		ImageButton img_replay, img_next, img_home;
		TextureRegionDrawable replayUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/menu_replay"));
		TextureRegionDrawable replayDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/menu_replay_pressed"));
		img_replay = new ImageButton(replayUp, replayDown);
		img_replay.setX(410);
		img_replay.setY(350);
		img_replay.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.LEVEL --;
				Constant.iCountClicked = PreferencesData.getCountClicked();
				HeaderGame.lb_Click.setText("Clicked: "+Constant.iCountClicked);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		TextureRegionDrawable nextUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/menu_next"));
		TextureRegionDrawable nextDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/menu_next_pressed"));
		img_next = new ImageButton(nextUp, nextDown);
		img_next.setX(660);
		img_next.setY(350);
		img_next.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				if(!Constant.b_custom){
					PreferencesData.setLevel(Constant.LEVEL);
					PreferencesData.setCountClicked(Constant.iCountClicked);
				}
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		TextureRegionDrawable homeUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/menu_home"));
		TextureRegionDrawable homeDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/menu_home_pressed"));
		img_home = new ImageButton(homeUp, homeDown);
		img_home.setX((Constant.STAGE_WIDTH - img_home.getWidth())/2);
		img_home.setY(img_next.getY() - img_next.getHeight() - 20);
		img_home.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				if(!Constant.b_custom){
					PreferencesData.setLevel(Constant.LEVEL);
					PreferencesData.setCountClicked(Constant.iCountClicked);
				}
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		group.addActor(img_bg);
		group.addActor(lb_result);
		group.addActor(img_replay);
		group.addActor(img_next);
		group.addActor(img_home);
		
		group.addAction(Actions.sequence(Actions.scaleTo(0f, 0f), Actions.scaleTo(1f, 1f, 0.3f)));
		return group;
	}
	
	static void setRow_Col(Label lb_cols, Label lb_rows, Label lb_cards){
		switch(Constant.LEVEL){
		case 1:
			lb_cols.setText("4");
			lb_rows.setText("2");
			lb_cards.setText("8");
			break;
		case 2:
			lb_cols.setText("6");
			lb_rows.setText("2");
			lb_cards.setText("12");
			break;
		case 3:
			lb_cols.setText("6");
			lb_rows.setText("3");
			lb_cards.setText("18");
			break;
		case 4:
			lb_cols.setText("6");
			lb_rows.setText("4");
			lb_cards.setText("24");
			break;
		case 5:
			lb_cols.setText("7");
			lb_rows.setText("4");
			lb_cards.setText("28");
			break;
		case 6:
			lb_cols.setText("8");
			lb_rows.setText("4");
			lb_cards.setText("32");
			break;
		case 7:
			lb_cols.setText("9");
			lb_rows.setText("4");
			lb_cards.setText("36");
			break;
		}
	}
	public static Group createWindow_CustomCards(){
		group = new Group();
		Image img_bgNen = new Image(Constant.atlas.findRegion("datagame/bg_popup"));
		
		Constant.LEVEL = 1;
		Image img_bg = new Image(Constant.atlas.findRegion("datagame/bg_custom_size"));
		final Label lb_rows;
		final Label lb_cols;
		final Label lb_cards;
		TextButton btn_Home, btn_Ok;
		ImageButton img_plus, img_minus;
		
		img_bg.setX((Constant.STAGE_WIDTH - img_bg.getWidth())/2);
		img_bg.setY((Constant.STAGE_HEIGHT - img_bg.getHeight())/2-10);

		lb_rows = new Label("2 ", Constant.skin, "label45_bold");
		lb_rows.setX(450);
		lb_rows.setY(470);
		
		lb_cols = new Label("4", Constant.skin, "label45_bold");
		lb_cols.setX(720);
		lb_cols.setY(470);
		
		lb_cards = new Label("8", Constant.skin, "label45_bold");
		lb_cards.setX(615);
		lb_cards.setY(405);
		
		TextureRegionDrawable plusUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/plus_icon"));
		TextureRegionDrawable plusDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/plus_icon_press"));
		img_plus = new ImageButton(plusUp, plusDown);
		img_plus.setX(460);
		img_plus.setY(290);
		img_plus.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.LEVEL ++;
				if(Constant.LEVEL > Constant.MAX_LEVEL)
					Constant.LEVEL = Constant.MAX_LEVEL;
				setRow_Col(lb_cols, lb_rows, lb_cards);
			}
			
		});
		
		TextureRegionDrawable minusUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/minus_icon"));
		TextureRegionDrawable minusDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/minus_icon_press"));
		img_minus = new ImageButton(minusUp, minusDown);
		img_minus.setX(680);
		img_minus.setY(290);
		img_minus.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.LEVEL --;
				if(Constant.LEVEL < 1)
					Constant.LEVEL =1;
				setRow_Col(lb_cols, lb_rows, lb_cards);
			}
			
		});
		
		btn_Home = new TextButton("Home", Constant.skin, "textButton_45_bold");
		btn_Home.setX(370);
		btn_Home.setY(150);
		btn_Home.setWidth(250);
		btn_Home.setHeight(100);
		btn_Home.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		btn_Ok = new TextButton("Ok", Constant.skin, "textButton_45_bold");
		btn_Ok.setX(630);
		btn_Ok.setY(150);
		btn_Ok.setWidth(250);
		btn_Ok.setHeight(100);
		btn_Ok.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.iCountClicked = 0;
				HeaderGame.lb_Click.setText("Clicked: 0");
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		group.addActor(img_bgNen);
		group.addActor(img_bg);
		group.addActor(img_minus);
		group.addActor(img_plus);
		group.addActor(lb_rows);
		group.addActor(btn_Ok);
		group.addActor(btn_Home);
		group.addActor(lb_cards);
		group.addActor(lb_cols);

		return group;
	}
	
	public static Group createWindow_ConfirmNewGame(){
		group = new Group();
		Image img_bgNen = new Image(Constant.atlas.findRegion("datagame/bg_popup"));
		Image img_bg = new Image(Constant.atlas.findRegion("datagame/bg_result"));
		img_bg.setWidth(600);
		img_bg.setX((Constant.STAGE_WIDTH - img_bg.getWidth())/2);
		img_bg.setY((Constant.STAGE_HEIGHT - img_bg.getHeight())/2 - 10);
		
		Label lb_ask = new Label("Clear all level ?", Constant.skin, "label45_bold");
		lb_ask.setX((Constant.STAGE_WIDTH - lb_ask.getWidth())/2);
		lb_ask.setY(460);
		
		TextButton btn_Yes, btn_No;
		btn_No = new TextButton("No", Constant.skin, "textButton_45_bold");
		btn_No.setX(400);
		btn_No.setY(210);
		btn_No.setHeight(100);
		btn_No.setWidth(210);
		btn_No.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		btn_Yes = new TextButton("Yes", Constant.skin, "textButton_45_bold");
		btn_Yes.setX(640);
		btn_Yes.setY(210);
		btn_Yes.setHeight(100);
		btn_Yes.setWidth(210);
		btn_Yes.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.LEVEL = 1;
				Constant.iCountClicked = 0;
				PreferencesData.setLevel(Constant.LEVEL);
				PreferencesData.setCountClicked(Constant.iCountClicked);
				HeaderGame.lb_Click.setText("Clicked: 0");
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		group.addActor(img_bgNen);
		group.addActor(img_bg);
		group.addActor(lb_ask);
		group.addActor(btn_No);
		group.addActor(btn_Yes);
		
		return group;
	}
	
	public static Group confirmExit(){
		group = new Group();
		Image img_bgNen = new Image(Constant.atlas.findRegion("datagame/bg_popup"));
		Image img_bg = new Image(Constant.atlas.findRegion("datagame/bg_result"));
		img_bg.setWidth(600);
		img_bg.setX((Constant.STAGE_WIDTH - img_bg.getWidth())/2);
		img_bg.setY((Constant.STAGE_HEIGHT - img_bg.getHeight())/2 - 10);
		
		Label lb_ask = new Label("Exit game ?", Constant.skin, "label45_bold");
		lb_ask.setX((Constant.STAGE_WIDTH - lb_ask.getWidth())/2);
		lb_ask.setY(460);
		
		TextButton btn_Yes, btn_No;
		btn_No = new TextButton("No", Constant.skin, "textButton_45_bold");
		btn_No.setX(400);
		btn_No.setY(210);
		btn_No.setHeight(100);
		btn_No.setWidth(210);
		btn_No.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		btn_Yes = new TextButton("Yes", Constant.skin, "textButton_45_bold");
		btn_Yes.setX(640);
		btn_Yes.setY(210);
		btn_Yes.setHeight(100);
		btn_Yes.setWidth(210);
		btn_Yes.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				System.gc();
				System.exit(1);
				Gdx.app.exit();
			}
			
		});
		
		group.addActor(img_bgNen);
		group.addActor(img_bg);
		group.addActor(lb_ask);
		group.addActor(btn_No);
		group.addActor(btn_Yes);
		
		return group;
	}
	
	public static Group createCategory(){
		group = new Group();
		Image img_bgNen = new Image(Constant.atlas.findRegion("datagame/bg_popup"));
		Image img_bg = new Image(Constant.atlas.findRegion("datagame/bg_selectThems"));
		img_bg.setX((Constant.STAGE_WIDTH - img_bg.getWidth())/2);
		img_bg.setY((Constant.STAGE_HEIGHT - img_bg.getHeight())/2 - 40);
		
		ImageButton img_sea, img_animal, img_fruit, img_flag;
		
		TextureRegionDrawable seaUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/sea_theme"));
		TextureRegionDrawable seaDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/sea_theme_press"));
		img_sea = new ImageButton(seaUp, seaDown);
		img_sea.setX(350);
		img_sea.setY(330);
		img_sea.addListener(new ClickListener(){

			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				PreferencesData.setCategory(Constant.iSeaCategory);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
			
		});
		
		TextureRegionDrawable fruitUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/fruit_theme"));
		TextureRegionDrawable fruitDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/fruit_theme_press"));
		img_fruit = new ImageButton(fruitUp, fruitDown);
		img_fruit.setX(700);
		img_fruit.setY(330);
		img_fruit.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				PreferencesData.setCategory(Constant.iFruitCategory);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
		});
		
		TextureRegionDrawable flagUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/flag_theme"));
		TextureRegionDrawable flagDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/flag_theme_press"));
		img_flag = new ImageButton(flagUp, flagDown);
		img_flag.setX(350);
		img_flag.setY(120);
		img_flag.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				PreferencesData.setCategory(Constant.iFlagCategory);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
		});
		
		TextureRegionDrawable animalUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/animal_theme"));
		TextureRegionDrawable animalDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/animal_theme_press"));
		img_animal = new ImageButton(animalUp, animalDown);
		img_animal.setX(700);
		img_animal.setY(130);
		img_animal.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				PreferencesData.setCategory(Constant.iAnimalCategory);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
		});
		
		group.addActor(img_bgNen);
		group.addActor(img_bg);
		group.addActor(img_sea);
		group.addActor(img_fruit);
		group.addActor(img_animal);
		group.addActor(img_flag);
		
		return group;
	}
}
